package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerPlanDowntimeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by shuan on 2017/1/19.
 */
@Controller
public class PartnerPlanDowntimeController {
    
    @Resource
    TenantService tenantService;
    
    @Resource
    PartnerPlanDowntimeService partnerPlanDowntimeService;
    
    @RequestMapping(value = "/partner/{tenant}/api/v1/planDowntime/list",method = RequestMethod.GET)
    @ResponseBody
    public Map<String ,Object> getPlanDowntimeList(@PathVariable("tenant")String tenantKey,
                                                   @RequestParam(name = "deviceId") String deviceId){
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String,Object>> list = partnerPlanDowntimeService.getDevicePlanDowntime(tenantId,deviceId);

        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(list)
                .build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/planDowntime/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> planDowntimeAdd(@PathVariable("tenant")String tenantKey,
                                              @RequestBody Map<String, Object> data) throws ParseException {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        partnerPlanDowntimeService.addPlanDowntime(tenantId,data.get("deviceId").toString(),data.get("startTime").toString(),data.get("endTime").toString());
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/planDowntime/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> planDowntimeDelete(@PathVariable("tenant")String tenantKey,
                                                 @RequestBody Map<String,Object>data){
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        partnerPlanDowntimeService.deletePlanDowntime(tenantId,data.get("deviceId").toString(),data.get("startTime").toString(),data.get("endTime").toString());
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }
}
